#include <myheader.h>

/*Usage:  */
int main(int argc, char *argv[])
{
    //create shared memory
    key_t key = ftok("gdb_fork",10);
    ERROR_CHECK(key,-1,"ftok");

    long num = 0;
    int shmid = shmget(key,sizeof(long),IPC_CREAT|0666);
    ERROR_CHECK(shmid,-1,"shmget");


    long *ptr = (long*)shmat(shmid,NULL,0);
    ERROR_CHECK(ptr,(void*)-1,"shmat");

    *ptr = num;
    if(fork()==0){
        for(int i = 0; i < 10000000; i++){
            __atomic_add_fetch(ptr, 1, __ATOMIC_SEQ_CST);
        }
        shmdt(ptr);

    }else{
        
        for(int i = 0; i < 10000000; i++){
            __atomic_add_fetch(ptr, 1, __ATOMIC_SEQ_CST);
        }
        printf("%ld\n",*ptr);
        shmdt(ptr);
        wait(NULL);
    }
return 0;
}

